<?php
namespace app\service;

use think\Db;

class UserRepeat {
    /**
     * @param $user_id
     * @return mixed
     */
    public function beforeAction($user_id) {
        $result = Db::name('user_repeat_check')->master()->where(['user_id' => $user_id])->find();
        if (!$result) {
            $result = [
                'user_id'                => $user_id,
                'device_ids'             => '',
                'zhifucounts'            => '',
                'wechat_openid'          => '',
                'qq_openid'              => '',
                'ips'                    => '',
                'pay_ids'                => '',
                'os'                     => '',
                'last_login_app_version' => 0,
                'device_brand'           => '',
            ];
            Db::name('user_repeat_check')->insert($result);
        }
        return $result;
    }

    /**
     * @param $user_id
     * @param $key
     * @param $value
     * @param $openid
     * @param $wx
     */
    public static function InsertS($user_id, $key, $value, $openid = '', $wx = 0) {
        $item   = (new UserRepeat)->beforeAction($user_id);
        $update = ['sys_channel'=>1];

        if (in_array($key, ['os', 'device_brand', 'last_login_app_version'])) {
            $update[$key] = $value;
        } else {
            $data = $item[$key] ? explode(',', $item[$key]) : [];
            if (!in_array($value, $data)) {
                array_push($data, $value);
                $data   = implode(',', $data);
                $update = [$key => $data];
            }

            if ($key == 'ips' && 1==3) {//暂时关闭ip风控
                $ips = ['171.34.209.179', '171.34.226.60'];
                $fips = ['221.228.187', '122.193.198'];
                // $fips = [];
                $ips = array_merge($ips, $fips);
                foreach ($ips as $ipk => $ipv) {
                    if (strpos($value, $ipv) === 0) {
                        $user = Db::name('user')->where(['reg'=>1,'id'=>$user_id])->find();
                        if ($user && $user['is_banned'] == 0) {
                            if (in_array($ipv, $fips)) {
                                if ($user['sex'] == '女' || strtotime('-7 day') > strtotime($user['register_datetime'])) {
                                    continue;
                                }
                            }
                            $desc  = 'ip:' . $value;
                            $haswr = Db::name('warn_user_records')->master()->where(['user_id' => $user_id, 'function' => $desc])->whereTime('time', 'today')->find();
                            if (!$haswr) {
                                Db::name('warn_user_records')->insert(['user_id' => $user_id, 'function' => $desc, 'time' => time()]);
                            }
                            $banned_end_time = date('Y-m-d H:i:s', strtotime('+3 years'));
                            Db::name('user')->where(['id' => $user_id])->update(['is_banned' => 1, 'banned_end_time' => $banned_end_time, 'banned_desc' => '账号异常']);
                            Db::name('config_personal')->where(['user_id' => $user_id])->update(['is_read_rule' => 0]);
                            
                            Db::name('admin_logs')->insert([
                                'channel' => 3,
                                'toward' => 0,
                                'money' => 0,
                                'user_id' => $user_id,
                                'content' => 'IP风控,封禁账号至' . $banned_end_time
                            ]);
                            // Privatechat::notice($value['user_id'], '封禁账号至' . $banned_end_time . ' 原因：账号异常', json_encode(['type'=>1, 'content'=>'申请解除封号','state'=>0], JSON_UNESCAPED_UNICODE), 1, -3);
                        }

                    }
                }
            }
        }
        if ($openid) {
            $ops               = ['wechat_openid', 'qq_openid'];
            $update[$ops[$wx]] = $openid;
        }
        if (sizeof($update)) {
            try {
                Db::name('user_repeat_check')->where(['user_id' => $user_id])->update($update);
            } catch (\Exception $e) {

            }
        }
    }
}
